package com.dzt.androidframework.json.car;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.dzt.androidframework.R;
import com.dzt.androidframework.activity.ActivityTitle;
import com.dzt.androidframework.adapter.AbsListAdapter;
import com.mob.mobapi.API;
import com.mob.mobapi.APICallback;
import com.mob.mobapi.MobAPI;
import com.mob.mobapi.apis.Car;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CarSeriesActivity extends ActivityTitle implements APICallback {

	@ViewInject(R.id.lv_series)
	private ListView lvSeries;
	private MyAdapter adapter;
	private List<CarSeriesInfo> datas = new ArrayList<>();

	@Override
	protected int getLayoutId() {
		return R.layout.activity_car_series;
	}

	@Override
	protected void initParams() {
		String name = getIntent().getStringExtra("name");
		setTitleView(name);
		adapter = new MyAdapter(this, datas);
		lvSeries.setAdapter(adapter);
		lvSeries.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
				CarSeriesInfo info = datas.get(i);
				Intent intent = new Intent();
				intent.putExtra("cid", info.carId);
				intent.putExtra("name", info.seriesName);
				startActivity(CarSeriesDetailsActivity.class, intent);
			}
		});
		Car api = (Car) MobAPI.getAPI(Car.NAME);
		api.querySeriesName(name, this);
		showWaitDialog("加载中...", false, null);
	}

	@Override
	protected void initData(Bundle savedInstanceState) {

	}

	@Override
	protected String[] initPermissions() {
		return new String[0];
	}

	@Override
	public void onSuccess(API api, int action, Map<String, Object> result) {
		datas.clear();
		dismissDialog();
		try {
			List<Map<String, Object>> res = (List<Map<String, Object>>) result.get("result");
			if (res == null || res.isEmpty()) {
				return;
			}
			for (Map<String, Object> tmp : res) {
				CarSeriesInfo info = new CarSeriesInfo();
				info.seriesName = (String) tmp.get("seriesName");
				info.brandName = (String) tmp.get("brandName");
				info.guidePrice = (String) tmp.get("guidePrice");
				info.carId = (String) tmp.get("carId");
				datas.add(info);
			}
			adapter.notifyDataSetChanged();
		} catch (Throwable t) {
			throw new RuntimeException(t);
		}
	}

	@Override
	public void onError(API api, int i, Throwable throwable) {
		dismissDialog();
	}

	private class MyAdapter extends AbsListAdapter<CarSeriesInfo> {

		public MyAdapter(Context context, List<CarSeriesInfo> datas) {
			super(context, datas);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = layoutInflater.inflate(R.layout.item_car_series_name, parent, false);
				x.view().inject(holder, convertView);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			CarSeriesInfo series = datas.get(position);
			holder.tvBrandName.setText(series.brandName);
			holder.tvGuidePrice.setText(series.guidePrice);
			holder.tvSeriesName.setText(series.seriesName);
			return convertView;
		}
	}

	class ViewHolder {
		@ViewInject(R.id.tvBrandName)
		TextView tvBrandName;
		@ViewInject(R.id.tvGuidePrice)
		TextView tvGuidePrice;
		@ViewInject(R.id.tvSeriesName)
		TextView tvSeriesName;
	}
}
